﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.IDAL;
using KPIS.GERP.GFMS.DALFactory;

// test only
using KPIS.GERP.GFMS.DAL;

namespace KPIS.GERP.GFMS.BLL
{
    [Serializable]
    public class B_FIM_MoneyRequests : IBLL_FIM_MoneyRequests
    {
        Common common = new Common();
        private static readonly IDAL_FIM_MoneyRequests dal = DataAccess.Create_FIM_MoneyRequests();
        //D_FIM_MAS_Receives r = new D_FIM_MAS_Receives();

        public B_FIM_MoneyRequests()
        {

        }

        public IList<M_FIM_MoneyRequests> GetFull(string bgm_source_seq
            , string bgm_year, string work_area_seq, string event_seq
            , string contact_seq, string emp_seq, string record_status, bool bReadyToPay)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> nwork_area_seq = common.ConvertNullInt(work_area_seq);
            Nullable<int> nevent_seq = common.ConvertNullInt(event_seq);
            Nullable<int> ncontact_seq = common.ConvertNullInt(contact_seq);
            Nullable<int> nemp_seq = common.ConvertNullInt(emp_seq);
            return dal.GetFull(nbgm_source_seq, nbgm_year, nwork_area_seq
                , nevent_seq, ncontact_seq, nemp_seq, record_status, bReadyToPay);
        }

        public IList<M_FIM_MoneyRequests> GetReturnDate(string bgm_source_seq
           , string bgm_year, string work_area_seq, string event_seq
           , string contact_seq, string emp_seq, string record_status, DateTime? dFromDate, DateTime? dToDate, bool bReadyToPay)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> nwork_area_seq = common.ConvertNullInt(work_area_seq);
            Nullable<int> nevent_seq = common.ConvertNullInt(event_seq);
            Nullable<int> ncontact_seq = common.ConvertNullInt(contact_seq);
            Nullable<int> nemp_seq = common.ConvertNullInt(emp_seq);
            return dal.GetReturnDate(nbgm_source_seq, nbgm_year, nwork_area_seq
                , nevent_seq, ncontact_seq, nemp_seq, record_status, dFromDate, dToDate, bReadyToPay);
        }

        public IList<M_FIM_MoneyRequests> GetList(string bgm_source_seq
            , string bgm_year, string work_area_seq, string event_seq
            , string contact_seq, string emp_seq, string filter)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> nwork_area_seq = common.ConvertNullInt(work_area_seq);
            Nullable<int> nevent_seq = common.ConvertNullInt(event_seq);
            Nullable<int> ncontact_seq = common.ConvertNullInt(contact_seq);
            Nullable<int> nemp_seq = common.ConvertNullInt(emp_seq);
            return dal.GetList(nbgm_source_seq, nbgm_year, nwork_area_seq
                , nevent_seq, ncontact_seq, nemp_seq, filter);
        }

        public IList<M_FIM_MoneyRequests> GetPetitionList(string bgm_source_seq
            , string bgm_year, string work_area_seq, string event_seq
            , string contact_seq, string emp_seq, string filter)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> nwork_area_seq = common.ConvertNullInt(work_area_seq);
            Nullable<int> nevent_seq = common.ConvertNullInt(event_seq);
            Nullable<int> ncontact_seq = common.ConvertNullInt(contact_seq);
            Nullable<int> nemp_seq = common.ConvertNullInt(emp_seq);
            return dal.GetPetitionList(nbgm_source_seq, nbgm_year, nwork_area_seq
                , nevent_seq, ncontact_seq, nemp_seq, filter);
        }

        public IList<M_FIM_MoneyRequests> GetNotPetition(string bgm_source_seq
            , string bgm_year, string petition_seq, string bgm_exp_grp_seq)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            Nullable<int> npetition_seq = common.ConvertNullInt(petition_seq);
            Nullable<int> nbgm_exp_grp_seq = common.ConvertNullInt(bgm_exp_grp_seq);
            return dal.GetNotPetition(nbgm_source_seq, nbgm_year, npetition_seq, nbgm_exp_grp_seq);
        }

        public IList<M_FIM_MoneyRequests> GetNotReturnList(string return_seq
            , string emp_seq, string contact_person_seq)
        {
            Nullable<int> nreturn_seq = common.ConvertNullInt(return_seq);
            Nullable<int> nemp_seq = common.ConvertNullInt(emp_seq);
            Nullable<int> ncontact_person_seq = common.ConvertNullInt(contact_person_seq);
            return dal.GetNotReturnList(nreturn_seq, nemp_seq, ncontact_person_seq);
        }

        //public string SelectRecentInsertedRoleSeq()
        //{
        //    return dal.SelectRecentInsertedRoleSeq();
        //}

        public M_FIM_MoneyRequests GetDnDeductInfo(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.GetDnDeductInfo(nSeq);
            }
            else
            {
                return null;
            }
        }

        public IList<M_HRM_PR_PayrollDtls> GetSalaryDeductList(string money_req_seq)
        {
            int nSeq;

            if (money_req_seq == null)
                return null;

            if (common.IsNumeric(money_req_seq))
            {
                nSeq = int.Parse(money_req_seq);
                return dal.GetSalaryDeductList(nSeq);
            }
            else
            {
                return null;
            }
        }

        public M_FIM_MoneyRequests GetInfo(string seq, string receive_seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.GetInfo(nSeq, common.ConvertNullInt(receive_seq));
            }
            else
            {
                return null;
            }
        }

        public M_FIM_MoneyRequests GetInfo(string seq)
        {
            int nSeq;
 
            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.GetInfo(nSeq);
            }
            else
            {
                return null;
            }
        }

        public M_FIM_MoneyRequests GetMoneyReqDNInfo(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.GetMoneyReqDNInfo(nSeq);
            }
            else
            {
                return null;
            }
        }

        public M_FIM_MoneyRequests GetNotReturnInfo(string money_req_seq, string return_seq)
        {
            Nullable<int> nmoney_req_seq = common.ConvertNullInt(money_req_seq);
            Nullable<int> nreturn_seq = common.ConvertNullInt(return_seq);
            return dal.GetNotReturnInfo(nmoney_req_seq, nreturn_seq);
        }

        public bool HaveAmountExist(string money_req_seq)
        {
            int nSeq;

            if (money_req_seq == null)
                return false;

            if (common.IsNumeric(money_req_seq))
            {
                nSeq = int.Parse(money_req_seq);
                return dal.HaveAmountExist(nSeq);
            }
            else
            {
                return false;
            }
        }

        public string Insert(M_FIM_MoneyRequests info)
        {
            try
            {
                return dal.Insert(info);
            }
            catch
            {
                throw;
            }
        }

        public void Update(M_FIM_MoneyRequests info)
        {
            try
            {
                dal.Update(info);
            }
            catch
            {
                throw;
            }
        }

        public void UpdateBidder(M_FIM_MoneyRequests info)
        {
            try
            {
                dal.UpdateBidder(info);
            }
            catch
            {
                throw;
            }
        }

        public void Delete(M_FIM_MoneyRequests info)
        {
            try
            {
                dal.Delete(info);
            }
            catch
            {
                throw;
            }
        }

        public void UpdateBgmExpGrpSeq(M_FIM_MoneyRequests info)
        {
            try
            {
                dal.UpdateBgmExpGrpSeq(info);
            }
            catch
            {
                throw;
            }
        }

        public void UpdateGlTranSeq(M_FIM_MoneyRequests info)
        {
            try
            {
                dal.UpdateGlTranSeq(info);
            }
            catch
            {
                throw;
            }
        }

        public bool UpdateBorrowAllow(M_FIM_MoneyRequests info)
        {
            try
            {
                if (!dal.HaveAmountExist(info.money_req_seq))
                {
                    return false;
                }
                else
                {
                    dal.UpdateBorrowAllow(info);
                    return true;
                }
            }
            catch
            {
                throw;
            }
        }

        public bool HaveAmountExist(int money_req_seq)
        {
            return dal.HaveAmountExist(money_req_seq);
        }

        public void UpdateBorrowReturnDate(int money_req_seq, DateTime real_pay_date)
        {
            dal.UpdateBorrowReturnDate(money_req_seq, real_pay_date);
        }

        public void UpdateUseAdvance(string money_req_seq, string use_advance, string remarks, string updated_by)
        {
            try
            {
                dal.UpdateUseAdvance(money_req_seq, use_advance, remarks, updated_by);
            }
            catch
            {
                throw;
            }
        }

        public bool ChangeStatus(M_FIM_MoneyRequests info)
        {
            string problemMessage;
            return ChangeStatus(info, out problemMessage);
        }

        public bool ChangeStatus(M_FIM_MoneyRequests info, out string problemMessage)
        {
            try
            {
                M_FIM_MoneyRequests model = dal.GetInfo(info.money_req_seq);

                if (info.record_status != "A")
                {
                    dal.ChangeStatus(info);
                    problemMessage = null;
                    return true;
                }
                else if (!dal.HaveAmountExist(info.money_req_seq) && (model.event_seq != 31 && model.event_seq != 32 && model.event_seq != 34))
                {
                    problemMessage = "จำนวนเงินคงเหลือไม่เพียงพอ";
                    return false;
                }
                else
                {
                    problemMessage = null;
                    dal.ChangeStatus(info);
                    return true;
                }
            }
            catch
            {
                throw;
            }
        }

        public DataTable Report_Contract(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.Report_Contract(nSeq);
            }
            else
            {
                return null;
            }
        }

        public DataTable Report_ContractSub(string seq)
        {
            int nSeq;

            if (seq == null)
                return null;

            if (common.IsNumeric(seq))
            {
                nSeq = int.Parse(seq);
                return dal.Report_ContractSub(nSeq);
            }
            else
            {
                return null;
            }
        }

        public string GetNextRefNo(string bgm_year, string bgm_source_seq)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            return dal.GetNextRefNo(nbgm_year, nbgm_source_seq);
        }

        public string GetNextBorrowNo(string bgm_year, string bgm_source_seq, string use_advance)
        {
            Nullable<int> nbgm_source_seq = common.ConvertNullInt(bgm_source_seq);
            Nullable<int> nbgm_year = common.ConvertNullInt(bgm_year);
            return dal.GetNextBorrowNo(nbgm_year, nbgm_source_seq, use_advance);
        }
    }
}
